package org.ReportDesigner.configurator;


import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class DesignerConfiguration implements Configuration{

    private static final Logger log = Logger.getLogger(DefaultConfiguration.class);

    private static Map<String, String> configAttr;
    private File confFile;


    static {
        configAttr = new HashMap<String, String>();
    }


    public DesignerConfiguration(File file){
        confFile = file;
    }

    public DesignerConfiguration(){

    }

    public void createConfiguration(){
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            SAXParser saxParser = saxParserFactory.newSAXParser();
            ConfigHandler handler = new ConfigHandler();
            saxParser.parse(confFile, handler);
            configAttr = handler.getConfiguration().getConfigMap();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  //.
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String,String> getConfigMap() {
        return configAttr;
    }


    @Override
    public void addAttribute(String name, String value) {
        configAttr.put(name, value);
    }

    @Override
    public String getAttribute(String name) {
        return configAttr.get(name);
    }

    @Override
    public String getFont() {
        return configAttr.get("font");
    }

    @Override
    public String getFontSize() {
        return configAttr.get("size");
    }

    @Override
    public String getColor() {
        return configAttr.get("color");
    }

    @Override
    public String getHeight() {
        return configAttr.get("height");
    }

    @Override
    public String getWidth() {
        return configAttr.get("width");
    }

    @Override
    public String getInsetsLeft() {
        return configAttr.get("left");
    }

    @Override
    public String getInsetsRight() {
        return configAttr.get("right");
    }

    @Override
    public String getInsetsTop() {
        return configAttr.get("top");
    }

    @Override
    public String getInsetsLowers() {
        return configAttr.get("lower");
    }
    @Override
    public String getFontEncoding(){
        return configAttr.get("encoding");
    }
    @Override
    public String getFontPath(){
        return configAttr.get("font-path");
    }

    public void setConfFile(File confFile) {
        this.confFile = confFile;
    }
}

class ConfigHandler extends DefaultHandler {

    private static final Logger log = Logger.getLogger(ConfigHandler.class);

    private Configuration configuration;
    private String attrName;
    private String attrValue;

    /**
     *
     * @param uri
     * @param localName
     * @param qName  - имя элемента XML документа
     * @param attributes - атрибуты элемента XML жокумента
     */
    public void startElement(String uri, String localName,
                             String qName, Attributes attributes){

        if (qName.equalsIgnoreCase("configuration")){
            configuration = new DefaultConfiguration();
        }
        else {
            attrName = qName;
        }


    }

    /**
     *
     * @param URI
     * @param localName
     * @param qName - имя элемента XML документа
     */
    public void endElement(String URI, String localName,
                           String qName){
        configuration.addAttribute(attrName, attrValue);
    }

    /**
     *
     * @param ch - массив симвалов считанной строки
     * @param start - начальный элемент
     * @param length - колличество символов
     */
    public void characters(char ch[], int start, int length){
          attrValue =  new String(ch, start, length);

    }

    public void startDocument(){
        log.info("Sax parsing document started.");
    }


    public void endDocument(){
        log.info("Sax parsing document ended.");
    }

    Configuration getConfiguration() {
        return configuration;
    }
}
